package com.firefly.service.command.common;

import com.aliyun.oss.*;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;
import com.firefly.util.handle.FireflyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;

/**
 * @description: 阿里云oss服务方法
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2023/5/15
 **/
@Component
@Slf4j
public class AliyunOssCommand {
    @Value("${aliyun.accesskeyid}")
    private String accessKeyId;

    @Value("${aliyun.secret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    public OSS createClient() {
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    /**
     * 文件流上传
     * inputStream 文件流
     * fileName 保存的文件路径及名称
     */
    public PutObjectResult uploadStream(InputStream inputStream, String fileName){
        PutObjectResult result = null;
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);
        // 设置该属性可以返回response。如果不设置，则返回的response为空。
        putObjectRequest.setProcess("true");
        // 创建PutObject请求。
        OSS ossClient = this.createClient();
        result = ossClient.putObject(putObjectRequest);
        if (result.getResponse().getStatusCode() != 200) {
            throw new FireflyException(500,"OSS文件上传失败！" + result.getResponse().toString());
        }
        ossClient.shutdown();
        return result;
    }
    /**
     * 本地文件上传
     * filePath 完整文件路径
     * fileName 保存的文件路径及名称
     */
    public PutObjectResult uploadFile(String filePath, String fileName){
        PutObjectResult result = null;
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, new File(filePath));
        // 设置该属性可以返回response。如果不设置，则返回的response为空。
        putObjectRequest.setProcess("true");
        // 创建PutObject请求。
        OSS ossClient = this.createClient();
        result = ossClient.putObject(putObjectRequest);
        if (result.getResponse().getStatusCode() != 200) {
            throw new FireflyException(500,"OSS文件上传失败！" + result.getResponse().toString());
        }
        ossClient.shutdown();
        return result;
    }
    /**
     * 临时文件形式的文件上传
     * filePath 完整文件路径
     * fileName 保存的文件路径及名称
     */
    public PutObjectResult uploadTmpFile(File file, String fileName){
        PutObjectResult result = null;
        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, file);
        // 设置该属性可以返回response。如果不设置，则返回的response为空。
        putObjectRequest.setProcess("true");
        // 创建PutObject请求。
        OSS ossClient = this.createClient();
        result = ossClient.putObject(putObjectRequest);
        if (result.getResponse().getStatusCode() != 200) {
            throw new FireflyException(500,"OSS文件上传失败！" + result.getResponse().toString());
        }
        ossClient.shutdown();
        return result;
    }
}
